package com.lmc.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.WebActivity;
import com.lmc.entity.Faq;

import org.w3c.dom.Text;

import java.util.List;

public class FaqAdapter extends RecyclerView.Adapter<FaqAdapter.ViewHolder> {
    Context context;
    List<Faq.DataBean> list;

    public FaqAdapter(Context context, List<Faq.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public FaqAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FaqAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.lmc_faq_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull FaqAdapter.ViewHolder holder, final int position) {
        holder.text.setText(list.get(position).getFirst_directory());
        holder.listView.setAdapter(new Adapter(list.get(position).getResult(), context));
        if (list.get(position).tag == 1) {
            holder.listView.setVisibility(View.VISIBLE);
            Drawable drawable = context.getResources().getDrawable(R.drawable.lmc_icon2);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.text.setCompoundDrawables(null, null, drawable, null);
        } else {
            holder.listView.setVisibility(View.GONE);
            Drawable drawable = context.getResources().getDrawable(R.drawable.lmc_icon1);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.text.setCompoundDrawables(null, null, drawable, null);
        }
        holder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).tag = list.get(position).tag == 1 ? 0 : 1;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        ListView listView;

        public ViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            listView = itemView.findViewById(R.id.listView);
        }
    }


    private class Adapter extends BaseAdapter {
        List<Faq.DataBean.ResultBean> list;
        Context context;

        public Adapter(List<Faq.DataBean.ResultBean> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Faq.DataBean.ResultBean getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ItemViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.lmc_faq_list_item_list_item, null);
                holder = new ItemViewHolder();
                holder.text = convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            } else {
                holder = (ItemViewHolder) convertView.getTag();
            }

            holder.text.setText("-" + list.get(position).getDirectory_name());
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WebActivity.start(context, list.get(position).getUrl(), "常见问题");

                }
            });
            return convertView;
        }
    }

    private class ItemViewHolder {
        TextView text;
    }

}
